<?php

namespace plugin\muadmin\app\admin\controller;

use process\Monitor;
use plugin\muadmin\app\common\base\BaseAdminController;
use plugin\muadmin\app\common\core\AppPluginCreate;
use plugin\muadmin\app\common\model\Plugin;
use think\facade\Db;

class Muplugin extends BaseAdminController
{
    public function initialize()
    {
        parent::initialize();
        $this->model = new Plugin();
        $pluginList = $this->model->where('status',1)->select();
        $this->assignConfig('pluginList',$pluginList);
    }
    public function add()
    {
        if($this->request->isAjax()){
            $data = $this->param('row');
            if(empty($data)){
                return $this->error("请填写插件信息");
            }
            $has = $this->model->where('key',$data['key'])->find();
            if(!empty($has)){
                return $this->error("插件已存在");
            }
            $obj = new AppPluginCreate();
            $obj->runHandle($data['key']);
            $res = $this->model->create($data);
            if(empty($res)){
                return $this->error("添加失败");
            }
            clearstatcache();
            $appconfig = config("plugin.{$data['key']}.app");
            $file = base_path().'/plugin/'.$data['key'].'/config/app.php';
//            $appconfig['controller_suffix'] = '';
            $appconfig['version'] = $data['version'];
            $appconfig['title'] = $data['title'];
            $appconfig['description'] = $data['description'];
            $appconfig['require'] = $data['require'];
            $appconfig['key'] = $data['key'];
            $fileStr = "<?php\nuse support\Request;\nreturn ".var_export($appconfig,true).";";
            file_put_contents($file,$fileStr);
            clearstatcache();
            return $this->success("添加成功");
        }
        return parent::add();
    }

    /**
     * 检测插件key
     * @return \support\Response
     */
    public function check()
    {
        $key = $this->param('key');
        if(empty($key)){
            return $this->error("请填写插件key");
        }
        $has = $this->model->where('key',$key)->find();
        if(!empty($has)){
            return $this->error("插件已存在");
        }
        return $this->success("插件不存在");
    }
    public function del()
    {
        $ids = $this->param('ids');
        if(empty($ids)){
            return $this->error("请选择要删除的插件");
        }
        $plugins = $this->model->whereIn('id',$ids)->select();
        $whereOr = [
        ];
        foreach ($plugins as $plugin){
            $temlike =[
                "%,{$plugin['key']}",
                "{$plugin['key']},%",
                "%,{$plugin['key']},%",
                "{$plugin['key']}",
            ];
            foreach ($temlike as $like){
                $whereOr[] = ['require','LIKE',$like];
            }
        }
//        print_r($whereOr);
//        $whereOr = [
//            ['require','like','test,%']
//        ];
        $yilai = $this->model->whereNotIn('id',$ids)
            ->where(function ($query)use($whereOr){
                $query->whereOr($whereOr);
            })->count();
//        return $this->success("删除成功",['num'=>$yilai]);
        if($yilai>0){
            return $this->error("请先删除依赖的插件");
        }
        clearstatcache();
        foreach ($plugins as $plugin){
            $temdir = base_path().'/plugin/'.$plugin['key'].'/';
            if(is_dir($temdir)){
                delete_dir($temdir);
            }
        }
        clearstatcache();
        $data = $this->request->all();
        if(empty($data['ids'])){
            return $this->error('请选择删除项');
        }
        $ids = $data['ids'];
        if(is_string($ids)){
            $ids = explode(',',$ids);
        }
        $res = $this->model->destroy($ids);
        reloadWebman();
        return parent::del();
    }

    /**
     * 本地安装
     * @return void
     */
    public function location()
    {

    }
}